package wish.stages;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import navigation.textTransformer.DummyTextTransformer;
import navigation.textTransformer.TextTransformer;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.HtmlSerializer;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.lobobrowser.html.domimpl.HTMLElementBuilder.Img;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;

import util.nodecloner.ChildFinder;


public class ParsingStage {

	public List<Parser> ParseWebSite(List<String> htmlDocs) throws ParserException{
		
		List<Parser> ret = new ArrayList<Parser>();
		
		for (String htmlDoc : htmlDocs) {
			
			ret.add(new Parser(new Lexer(new Page(htmlDoc, "UTF-8"))));
			
		}
		
		return ret;
	}
	
	public static void main(String[] args) throws IOException, ParserException {
		
		String html1 = "/home/pjbarrio/Documents/Wrapper/Website/1.html";
		
		String html1Content = readFile(new File(html1));
		
//		String html2 = "/home/pjbarrio/Documents/RelationExtractionStuff/DatabasesInterfaces/ResultsPage/2.html"
		
		List<String> htmlContent = new ArrayList<String>();
		
		htmlContent.add(html1Content);
		
		List<Parser> list = new ParsingStage().ParseWebSite(htmlContent);
		
		NodeIterator nl = list.get(0).elements();
		
		while (nl.hasMoreNodes()){
			
			Node node = nl.nextNode();
			
			System.out.println(node.toString());
			
		}
		
	}
	
	public static String readFile(File file) throws IOException {
		
		
		String ret = FileUtils.readFileToString(file);
		
		ret = StringEscapeUtils.unescapeHtml3(ret);
		
		if (ret == null)
			return "";
		
		
//		Tidy tidy = new Tidy();
//		tidy.setQuiet(true);
//		tidy.setShowWarnings(false);
//		tidy.setForceOutput(true);
//	
//		StringWriter sw = new StringWriter();
//		
//		Document document = tidy.parseDOM(new BufferedReader(new StringReader(ret)), 
//				sw);

		CleanerProperties props = new CleanerProperties();

		HtmlCleaner cleaner = new HtmlCleaner(props);
		
		org.htmlcleaner.TagNode node = cleaner.clean(ret);
	
		HtmlSerializer htmlSerializer = new SimpleHtmlSerializer(props);
		
		StringWriter sw = new StringWriter();
		
		htmlSerializer.write(node, sw, "UTF-8");
		
		return sw.toString();
		
	}
	
	public static String recoverText(Node node, boolean imageIsText, TextTransformer tt) {
		
		if (node instanceof TextNode){
			return tt.transformText(StringEscapeUtils.unescapeHtml3(node.getText()));
		}else if (node instanceof ImageTag){
			
			String ret = ((ImageTag)node).getAttribute("alt");
			
			if (ret != null)
				ret = tt.transformImageText(StringEscapeUtils.unescapeHtml3(ret));
			else
				ret = "";
			
			if (imageIsText)
				return ret + " " + tt.transformImageFile(StringEscapeUtils.unescapeHtml3(((ImageTag) node).getImageURL()));
			else
				return ret;
				
		}else{
			
			String ret = "";
			
			NodeList child = ChildFinder.getChildren(node);
			
			for (int i = 0; i < child.size(); i++) {
				
				ret += recoverText(child.elementAt(i),imageIsText,tt);
				
			}
			
			return ret;
		}
		
	}

	public static String recoverText(Node node){
		return recoverText(node, false, new DummyTextTransformer());
	}

	public static String recoverText(Node node, boolean b) {
		return recoverText(node, b, new DummyTextTransformer());
	}
	
}
